import resolve from 'rollup-plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import json from 'rollup-plugin-json';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
import { eslint } from 'rollup-plugin-eslint';
import { uglify } from 'rollup-plugin-uglify';
import liveServer from 'rollup-plugin-live-server';

const mode = process.env.NODE_ENV;
const isWatch = process.env.ROLLUP_WATCH;

console.log('执行环境：', mode);

const isProd = mode === 'production';
const onwarn = (warning, onwarn) => (warning.code === 'CIRCULAR_DEPENDENCY' && /[/\\]@sapper[/\\]/.test(warning.message)) || onwarn(warning);

export default {
  input : 'src/index.js',
  output: {
    file   : isProd ? 'dist/nav-menu.js' : 'test/nav-menu.js',
    format : 'iife',
    name   : 'NavMenu',
    globals: {
      layui: 'layui'
    },
    sourcemap: true
  },
  external: ['layui'],
  plugins : [
    json(),
    eslint(),
    replace({
      'process.env.NODE_ENV': JSON.stringify(mode)
    }),
    resolve(),
    babel({
      runtimeHelpers: true,
      exclude       : ['node_modules/**']
    }),
    commonjs(),
    isWatch && liveServer({
      port: 3000,
      root: 'test',
      file: 'index.html',
      open: false,
      wait: 500
    }),
    isProd && uglify({
      compress: {
        pure_getters: true,
        unsafe      : true,
        unsafe_comps: true
      }
    })
  ],
  onwarn
};
